//
//  IWSearchView.m
//  WeiBo17
//
//  Created by teacher on 15/8/17.
//  Copyright (c) 2015年 itcast. All rights reserved.
//

#import "IWSearchView.h"

@interface IWSearchView()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *textField;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *textFieldRigthCon;

@property (weak, nonatomic) IBOutlet UIButton *cancelButton;


@end

@implementation IWSearchView

+ (instancetype)searchView{
    return [[[NSBundle mainBundle] loadNibNamed:@"IWSearchView" owner:nil options:nil] lastObject];
}

/**
 *  这个方法是控件通过代码创建的话,必须要执行的方法
 *
 *  @param frame <#frame description#>
 *
 *  @return <#return value description#>
 */
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}


/**
 *  子控制连线完成
 */
- (void)awakeFromNib{
    NSLog(@"%s---%@",__func__,self.textField);
    
    //通过 initWithImage的方法初始化imageView的话,这个imageView是有大小
    UIImageView *leftView = [[UIImageView alloc] init];
    leftView.image = [UIImage imageNamed:@"searchbar_textfield_search_icon"];
    //设置imageView的contentMode
    leftView.contentMode = UIViewContentModeTop;
        
    //leftView为正方形
    leftView.width = self.textField.height;
    leftView.height = self.textField.height;
    
    //设置leftView与left的显示模式
    self.textField.leftView = leftView;
    self.textField.leftViewMode = UITextFieldViewModeAlways;
    self.textField.delegate = self;
    
    
//    self.textField.layer.cornerRadius = 8;
//    self.textField.layer.masksToBounds = YES;
//    
//    self.textField.layer.borderWidth = 3;
//    self.textField.layer.borderColor = [UIColor orangeColor].CGColor;
    
}

/**
 *  从xib里面加载出来
 *
 *  @param aDecoder <#aDecoder description#>
 *
 *  @return <#return value description#>
 */
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {
        NSLog(@"%s---%@",__func__,self.textField);
    }
    return self;
}


- (IBAction)cancelButtonClick:(UIButton *)sender {
//    [self.textField resignFirstResponder];
    [self endEditing:YES];
    self.textFieldRigthCon.constant = 0;
    [UIView animateWithDuration:0.25 animations:^{
        //如果有需要,就去重新布局
        [self layoutIfNeeded];
    }];

}


#pragma mark - textField delegate

- (void)textFieldDidBeginEditing:(UITextField *)textField{
    NSLog(@"%s",__func__);
    //更新约束,是不是就需要重新布局
    self.textFieldRigthCon.constant = self.cancelButton.width;
    [UIView animateWithDuration:0.25 animations:^{
        //如果有需要,就去重新布局
        [self layoutIfNeeded];
    }];
}




@end
